<?php include "classes.inc" ?>
<?php include "constants.inc" ?>
<?php session_start(); ?>
<?php

  if (isset($_GET['match'])) {
    $userInput = $_GET['match'];
  }

  $connection = mysql_connect(DB_SERVER, DB_USER, DB_PWD);
  mysql_select_db(DB_CATALOG);

	if (isset($dep)&& !empty($dep)) {
  		
		$dep = substr($dep,0,1);
		
		$query = "select te.team_id, te.team_name from zt_team te 
  			join zt_tournament t on te.tournament_id = t.tournament_id
  			where t.iscurrent = 1 and te.department_nr = ".$dep.
  			" order by te.team_name";
  	} else {
  		$query = "select te.team_id, te.team_name from zt_team te
  		join zt_tournament t on t.tournament_id = te.tournament_id
  		where t.iscurrent = 1 order by te.team_name";
  	}
  	
  	$result = mysql_query($query, $connection);
	
  	$resultTypeItems = array();
	while ($row = mysql_fetch_assoc($result)) {
		if ( isset($userInput) && !empty($userInput) ) {
			if (substr_compare($row['team_name'], $userInput, 0, 1) === 0) {
				$keys[] = $row['team_id'];
      			$values[] = $row['team_name'];
      			$resultTypeItems = array_combine($keys, $values);
			}
		} else {
			$keys[] = $row['team_id'];
      		$values[] = $row['team_name'];
      		$resultTypeItems = array_combine($keys, $values);
		}
    	
	}

  echo json_encode($resultTypeItems);
?>